package com.linkwings.httpconverter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class XStreamDateValueConverter implements Converter {

	DateFormat df = new SimpleDateFormat("dd/MM/yyyy");

    public void marshal(Object source, HierarchicalStreamWriter writer,
            MarshallingContext context) {
    	if(source !=null&& source instanceof Date){
    		Date d = (Date) source;
            writer.setValue(df.format(d));
    	}else{
    		writer.setValue("");
    	}

    }

    public Object unmarshal(HierarchicalStreamReader reader,
            UnmarshallingContext context) {
    	Date d=null;
		try {
			d = df.parse(reader.getValue());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return d;
    }

    @SuppressWarnings("unchecked")
    public boolean canConvert(Class type) {
        return true; // 不要检查类型
    }


}


